package spreadsheet.containers;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import common.gui.progress.IProgressItem;
import java.awt.BorderLayout;
import java.awt.Cursor;
import javax.swing.JInternalFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.InternalFrameListener;
import common.gui.progress.ProgressLabel;
import spreadsheet.gui.JeksJMenuBar;
import spreadsheet.spreadsheet.Table.Spreadsheet;
import spreadsheet.spreadsheet.tablemodel.UndoTableModel.UndoableTableModel;
import spreadsheet.storablespreadsheet.StorableDataFrame;

/**
 *
 * @author Nobody
 */
public final class SpreadsheetInternalFrame extends JInternalFrame implements IProgressItem, ISpreadsheetContainer {

    private int maximum;
    private ProgressLabel progressLabel = new ProgressLabel();
    private JPanel panel;
    private SpreadsheetScrollpane scrollPane = new SpreadsheetScrollpane();
    private JMenuBar menuBar = new JeksJMenuBar(getSpreadsheet());

    public SpreadsheetInternalFrame(String name) {
        this();
        setTitle(name);
        setName(name);
    }

    public SpreadsheetInternalFrame() {
        super();
        //   Helper.getHelper().setIconForJInternalFrame(this);
        createNewTabPane();
        setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setAutoscrolls(true);
        setClosable(true);
        setMaximizable(true);
        setIconifiable(true);
        setResizable(true);
        setBounds(70, 30, 450, 290);
    }

    public SpreadsheetInternalFrame(StorableDataFrame storableFrame) {
        this(storableFrame.getName());
        setBounds(storableFrame.getBounds());
        setLayer(storableFrame.getLayer());
        ((UndoableTableModel) getSpreadsheet().getModel()).importStorableModel(storableFrame.getStorableTableModel());
        afterReading();
        updateUI();
    }

    public InternalFrameListener getInternalFrameListener(Class<?> clazz) {
        for (InternalFrameListener frameListener : getInternalFrameListeners()) {
            if (frameListener.getClass().equals(clazz)) {
                return frameListener;
            }
        }
        return null;
    }

    public StorableDataFrame exportAsStorableDataFrame() {
        return new StorableDataFrame(this);

    }

    public Spreadsheet getSpreadsheet() {
        return scrollPane.getSpreadsheet();
    }

    @Override
    public void beforeReading() {
        this.setTitle(BEFORE_READING);
        this.setVisible(true);
        getProgressLabel().beforeReading();
        getSpreadsheet().setVisible(false);
    }

    @Override
    public void afterReading() {
        replaceProgressLabelWithFormulaField();
        scrollPane.setEnableTable(true);
        getSpreadsheet().setVisible(true);
        setTitle(getName());
        panel.updateUI();
    }

    private void replaceProgressLabelWithFormulaField() {
        getProgressLabel().afterReading();
        panel.remove(getProgressLabel());
        panel.add(scrollPane.getFormulaField(), BorderLayout.NORTH);

    }

    @Override
    public void readed(double readed) {
        setTitle(String.valueOf((int) ((float) readed / getMaximum() * 100)) + " % readed.");
        update(getGraphics());
        repaint();
    }

    @Override
    public void setMaximum(int maximum) {
        this.maximum = maximum;
    }

    @Override
    public int getMaximum() {
        return maximum;
    }

    public ProgressLabel getProgressLabel() {
        return progressLabel;
    }

    private void createNewTabPane() {
        panel = new JPanelExceptionCatcher(new BorderLayout());
        panel.add(progressLabel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);
        getContentPane().add(panel, BorderLayout.CENTER);
        setJMenuBar(menuBar);
        pack();
    }

    public void tablePasteAction() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                setEnabled(false);
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                scrollPane.getProgressTable().pasteCopiedData();
                setCursor(Cursor.getDefaultCursor());
                setEnabled(true);
            }
        });
    }
}

class JPanelExceptionCatcher extends JPanel {

    JPanelExceptionCatcher(BorderLayout borderLayout) {
        super(borderLayout);
    }

    @Override
    public void layout() {
        try {
            super.layout();
        } catch (java.util.NoSuchElementException ex) {
            //  ex.printStackTrace();
        }
    }
}
